/*
	经销商详情
	@ 展示 修改 删除
*/

var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');

$(function(){
	layui.use(['layer', 'form'], function(){
		var layer = layui.layer, form = layui.form;
		var ajaxData = { aId: adminLoginedInfo.aId, aToken: adminLoginedInfo.aToken };

		function currentAgentDetail(){
			/*
				@ 根据code编码获取经销商信息
			*/
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _ajaxData = $.extend({}, {wId: getParam('wId')}, ajaxData);
			$.ajax({
				url: BASE_SERVICEURL + URlS.agent,
				type: 'GET',
				data: _ajaxData,
				success: function(responseData){
					layer.close(loadIndex);
					if(responseData.msg.code == 200){
						//console.log(responseData.data)
						initForm(responseData.data.data);
					}else{
						layer.msg(responseData.msg.msg, {time: 2000})
					}
				},
				error: function(responseData){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000});
				}
			}); 
		}

		function modifyAgentData(modifyData){
			/*
				修改经销商数据
				@ modifyData 格式化表单数据
			*/
			loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var  _ajaxData = $.extend({}, modifyData, ajaxData);
			$.ajax({
				url: BASE_SERVICEURL + URlS.agent,
				type: 'POST',
				data: _ajaxData,
				success: function(responseData) {
					layer.close(loadIndex);
					if(responseData.msg.code == 200){
						location.reload();
					}else{
						layer.msg(responseData.msg.msg, {time: 2000});
					};
				},
				error: function(responseData){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
						window.location.href = './500.html';
					}});
				}
			})
		}

		function deleteAgentData(agentCode){
			/*
				删除经销商
				@ agentCode 经销商编码
			*/
			loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var  _ajaxData = $.extend({}, agentCode, ajaxData);
			$.ajax({
				url: BASE_SERVICEURL + URlS.agent,
				type:'DELETE',
				data: _ajaxData,
				success: function(responseData){
					layer.close(loadIndex);
					if(responseData.msg.code == 200){
						layer.msg(responseData.msg.msg, {time: 2000, end: function(){
							//关闭窗口并刷新父窗口
    						parent.location.reload();
						}})
					}else{
						layer.msg(responseData.msg.msg, {time: 2000});
					}
				},
				error: function(responseData){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
						window.location.href = './500.html';
					}});
				}
			})
		};

		function initForm(agentData){
			/*
				@ 初始化form表单
				@ agentData 代理商数据 数据格式为 array
			*/
			var agt = agentData[0];
			var fd = { 
				// form data
				agtNameCn: agt.agt_name['cn'],
				agtNameTw: agt.agt_name['tw'],
				agtNameEn: agt.agt_name['en'],
				agentImgLink: agt.agt_img,
				agtPhone: agt.agt_phone,
				/*agtFax: agt.agt_fax,*/
				openTime: agt.agt_open_daily,
				agtCode: agt.agt_code,
				agtMergeCn: agt.agt_merge['cn'],
				agtMergeTw: agt.agt_merge['tw'],
				agtMergeEn: agt.agt_merge['en'],
				addrCn: agt.agt_addr['cn'], 
				addrTw: agt.agt_addr['tw'],
				addrEn: agt.agt_addr['en'],
				lat: agt.agt_coordinates[0],
				lng: agt.agt_coordinates[1],
				status: agt.agt_status,
				descCn: agt.agt_intro['cn'],
				descTw: agt.agt_intro['tw'],
				descEn: agt.agt_intro['en'],
			};
			form.val('agent-detail-form', fd);
			form.render();
		};

		form.on('submit(modify)', function(data){
			/*
				保存修改表单数据
			*/
			layer.confirm(MESSAGES.confirm_modify, {title: '新增提示'}, function(index){
				var f = data.field;
				var md = {
					wId: getParam('wId'),
					name: langTransObj(f.agtNameCn, f.agtNameTw, f.agtNameEn),
					intro: langTransObj(f.descCn, f.descTw, f.descEn),
					addr: langTransObj(f.addrCn, f.addrTw, f.addrEn),
					phone: f.agtPhone,
					coordinates: f.lat + '|' + f.lng,
					time: f.openTime,
					status: f.status,
					fax: "----",
					img: f.agentImgLink,
					merge: langTransObj(f.agtMergeCn, f.agtMergeTw, f.agtMergeEn)
				};
				layer.close(index);
				modifyAgentData(md);
			});
			return false;
		});

		form.on('submit(delete)', function(){
			/*
				删除经销商 -- 不可恢复 请谨慎使用
			*/
			layer.confirm(MESSAGES.confirm_delete, {title: '删除提示'}, function(index){
				var c = {wId: getParam('wId')}
				deleteAgentData(c);
				layer.close(index);
			})
			return false;
		})

		currentAgentDetail();

	});
})




